<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/4/18
 * Time: 11:47
 */

namespace app\common\model;

use \fast\Form as fastForm;
use think\Template;
use think\View;

/**
 * Class Form
 * @package app\common\model
 * 用来生成一个全能的div包含的field
 */
class Form
{
    protected $view;
    public function __construct()
    {
        $this->view=View::instance();
    }

    public function open($options=[]){
        $options=array_merge($options,['class'=>'layui-form']);
        return fastForm::open($options);
    }
    public function close(){
        return fastForm::close();
    }
    protected function base($lable,$content){
        $html='';
        $html.=fastForm::divOpen(['class'=>'layui-form-item']);
        $html.=$lable;
        $html.=fastForm::divOpen(['class'=>'layui-input-block']);
        $html.=$content;
        $html.=fastForm::divClose();
        $html.=fastForm::divClose();
        return $html;
    }

    public function text($index,$label,$name,$value=null){
        $content=$this->view->fetch('common@tpl/text',['index'=>$index,'label'=>$label,'name'=>$name]);
        return $content;
    }
    public function select($index,$lable,$name,$list,$selected=null){
        $lable=fastForm::label('',$lable,['class'=>'layui-form-label']);
        $select=fastForm::select($name,$list,$selected,['lay-filter'=>'select_type']);
        return $this->base($lable,$select);
    }
    public function checkbox($index,$label,$name,$list=[],$checked=null){
        $lable=fastForm::label('',$label,['class'=>'layui-form-label']);
        $checkout='';
        foreach ($list as $v){
            $checkout.=fastForm::checkbox($name.'[]',$v['value'],$checked,['title'=>$v['name']]);
        }
        return $this->base($lable,$checkout);
    }
    public function radio($index,$label,$name,$list=[],$checked=null){
        $lable=fastForm::label('',$label,['class'=>'layui-form-label']);
        $html='';
        foreach ($list as $v){
            $html.=fastForm::radio($name,$v['value'],$checked,['title'=>$v['name']]);
        }
        return $this->base($lable,$html);
    }
    public function textarea($index,$label,$name,$value=''){
        $id=uniqid($name.'_');
        $lable=fastForm::label($id,$label,['class'=>'layui-form-label']);
        $textarea=fastForm::textarea($name,$value,['class'=>'layui-textarea']);
        return $this->base($lable,$textarea);
    }

    public function button(){
        $content=fastForm::button('提交',['class'=>'layui-btn','id'=>'submit','type'=>'button','lay-filter'=>'*']);
        $content.=fastForm::button('重置',['class'=>'layui-btn layui-btn-primary','type'=>'reset']);
        return $this->base('',$content);
    }
    public function multiInput($name){
        $html='';
        $html.=fastForm::divOpen(['class'=>'layui-form-item multi']);
        $html.=fastForm::divOpen(['class'=>'layui-inline']);
        $html.=fastForm::label('',$name,['class'=>'layui-form-label']);
        $html.=fastForm::divOpen(['class'=>'layui-input-inline']);
        $html.=fastForm::text('field[]',null,['class'=>'layui-input',
            'placeholder'=>'显示']);
        $html.=fastForm::divClose();
        $html.=fastForm::divOpen(['class'=>'layui-input-inline']);
        $html.='<button class="layui-btn layui-btn-sm layui-btn-primary add" type="button">添加</button>';
        $html.='<button class="layui-btn layui-btn-sm layui-btn-danger delete" type="button">删除</button>';
        $html.=fastForm::divClose();
        $html.=fastForm::divClose();
        $html.=fastForm::divClose();
        return $html;
    }
    public function hidden($name,$value){
        return fastForm::hidden($name,$value);
    }
    public function jqRadio($index,$label,$name,$list){
        $content=$this->view->fetch('common@tpl/radio',['index'=>$index,'label'=>$label,'name'=>$name,'list'=>$list]);
        return $content;
    }
    public function jqCheckBox($index,$label,$name,$list=[]){
        $content=$this->view->fetch('common@tpl/checkbox',['index'=>$index,'label'=>$label,'name'=>$name,'list'=>$list]);
        return $content;
    }
    public function jqTextarea($index,$label,$name){
        $content=$this->view->fetch('common@tpl/textarea',['index'=>$index,'label'=>$label,'name'=>$name]);
        return $content;
    }
    public function table($index,$label,$name,$list=[],$checked=[]){
        $content=$this->view->fetch('common@tpl/table',['index'=>$index,'label'=>$label,'name'=>$name,'list'=>$list]);
        return $content;
    }
}